Skip to content

Lint zeros in math functions by their type - #547

Open
Arron Eicholz (arronei) wants to merge 2 commits into
microsoft:mainfrom
arronei:css/zero-units-math-functions
Open

Arron Eicholz (arronei) wants to merge 2 commits into
microsoft:mainfrom
arronei:css/zero-units-math-functions

Conversation

@arronei

Copy link
Copy Markdown

Inside a math function, a number token is always a <number>. CSS Values 4: "unitless 0 <length>s aren't supported in math functions", and width: calc(0 + 5px) is invalid. So a zero length in a math function needs its unit. Today the linter gets this backwards in both directions:

.icon {
  margin-block: calc(var(--glyph-height, 0px) / -2);   /* valid, but zeroUnits says to drop the unit */
  margin-block: calc(var(--glyph-height, 0) / -2);     /* invalid, no warning */
}

var() substitutes its fallback, so with --glyph-height unset the second line is calc(0 / -2), a <number>, which margin-block doesn't accept. In Edge with --glyph-height: 12px and no --glyph-descent, vertical-align: calc(var(--glyph-height, 0px) / 2 - var(--glyph-descent, 0px)) computes to 6px, and the same declaration with 0 computes to baseline, because it is thrown out.

zeroUnits (src/services/lint.ts)

microsoft/vscode#46997 stopped the hint for a zero whose nearest enclosing function is calc. A zero in a var() fallback inside calc, zeros in min(), max(), clamp() and the other math functions, and CALC() still get it. Now any enclosing math function counts, compared case-insensitively. The list covers CSS Values 4 (calc, min, max, clamp, round, mod, rem, the trig, exponential and sign functions), -webkit-calc and -moz-calc, and the Values 5 functions calc-size, progress, media-progress, container-progress and random.

unitlessZeroInMath (new rule, warning; src/services/lint.ts, lintRules.ts)

It follows the spec's type checking and flags a unitless 0 that has to be a dimension:

  • With + or -, both sides need the same type, and min(), max(), clamp(), hypot(), round(), mod(), rem() and abs() need consistent arguments. So a 0 next to a dimension is flagged, wherever the math function is: calc(0 + 5px), max(0, 10vw), translate(calc(0 + 1px)).
  • A math function that is the whole value resolves to its calculation's type. If the property takes a length or percentage but not a number, the 0 that carries that type is flagged: width: calc(0), margin-block: calc(var(--h, 0) / -2).

What isn't flagged:

  • A 0 that stays a <number>: a multiplier (calc(0 * 10px), calc(var(--n, 0) * 1px)), a divisor, an argument of pow(), sign() and other number functions, or a value of a property that takes numbers (line-height: max(0, 1.5)).
  • Anything whose type can't be told. / may divide by a dimension (typed arithmetic), so calc(var(--a, 0) / var(--b)) is skipped. Expressions with Sass or Less variables are skipped too, because their math lets unitless numbers mix with lengths.

The parser chains + - * / left to right without precedence, so the check regroups them into products first. NumericValue.getValue() reads no sign, so -2 comes back as unit -2. The check reads the number from the text instead.

Property types come from restrictions. 262 properties have none, including margin-block, padding-inline, inset, gap and scroll-margin-*, so the check falls back to the MDN syntax (<'margin-top'>{1,2}) and resolves property references. stroke-width and stroke-dashoffset are listed as length-only but also take a <number>, so they're excluded.

Checks

  • Tests cover both sides, for CSS, LESS and SCSS.
  • 228 properties are treated as length or percentage only. Checked against mdn-data syntax, none of them accepts a <number> at the top level.
  • No hits on 204 real stylesheets with 5,143 math functions (Tailwind, VS Code and others from node_modules).

Result

Input zeroUnits before → after unitlessZeroInMath
margin-block: calc(var(--glyph-height, 0px) / -2) hint → no hint –
margin-block: calc(var(--glyph-height, 0) / -2) – warning
vertical-align: calc(var(--h, 0) / 2 - var(--d, 0)) – 2 warnings
CALC(0px - 10vw) hint → no hint –
width: max(0px, 10vw - 20px) hint → no hint –
width: max(0, 10vw - 20px) – warning
width: calc(0 + 5px), width: calc(0) – warning
width: calc(0 * 10px) – –
line-height: max(0, 1.5) – –
width: var(--w, 0px), translate(0px, 10px) hint → hint –

Follow-up: a css.lint.unitlessZeroInMath setting (and the scss/less ones) in microsoft/vscode extensions/css-language-features/package.json, so the rule shows up in Settings.

Inside a math function a number token is always a <number>, never a
"unitless 0" <length> (css-values-4, Type Checking). So a zero length
there needs its unit, and a unitless 0 where a length is needed makes
the declaration invalid.

zeroUnits: stop suggesting to drop the unit there. The rule only
skipped a zero whose nearest enclosing function is `calc`
(vscode#46997), so it still flagged:

- a var() fallback inside calc: calc(var(--h, 0px) / 2)
- zeros in min(), max(), clamp() and the other math functions
- CALC() in another case

Now any enclosing math function counts, compared case-insensitively.

unitlessZeroInMath (new, warning): flag a unitless 0 that has to be a
dimension:

- next to a dimension in + or -, or in min(), max(), clamp(), hypot(),
  round(), mod(), rem() or abs(): calc(0 + 5px), max(0, 10vw)
- in a math function that is the whole value of a property that takes
  a length or percentage but not a number:
  margin-block: calc(var(--h, 0) / -2)

var() fallbacks count, because var() substitutes them. A zero that is
a multiplier, a divisor or an argument of a number function stays a
<number> and isn't flagged. Expressions with Sass or Less variables are
skipped, because their math lets unitless numbers mix with lengths.

The property's type comes from its restrictions. Newer properties such
as margin-block, inset and gap have none, so it falls back to their MDN
syntax.
Arron Eicholz (arronei) added a commit to arronei/vscode-syntax that referenced this pull request Nov 15, 2026
Record the draft PR links in docs/upstream-prs.md and the README:
microsoft/vscode-custom-data#147 and #148, mdn/data#1121 and
microsoft/vscode-css-languageservice#547.

Drop the Claude attribution and session lines from the PR folders'
COMMIT_MSG.txt and PR.md, and update the doc-paths PR body: the Nov 12
web custom data update filled the cited descriptions by hand, but the
lookup still returns nothing on main.
@arronei
Arron Eicholz (arronei) marked this pull request as ready for review November 16, 2026 17:43
Copilot AI balanced review requested due to automatic review settings November 16, 2026 17:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

Code review approval is blocked because there is a critical null-safety bug in src/services/lint.ts where decl?.getFullPropertyName().toLowerCase() can still throw and crash the linter in cases like calc()/var() outside declarations, alongside a minor maintainability nit about an unclear "ponytail" comment tag.

2 open issues
What changed in this PR

Aligns CSS linter behavior with CSS Values typed math rules so unitless 0 is treated correctly inside math functions and introduces a targeted warning when a unit is required.

Changes:

  • Expand zeroUnits suppression so the linter no longer suggests dropping units for zeros inside any enclosing math function (case-insensitive).
  • Add a new unitlessZeroInMath lint rule to warn on unitless 0 only when the expression/property requires a dimension.
  • Add CSS and SCSS regression tests covering both the updated zeroUnits behavior and the new rule.
File Description
src/​test/​scss/​lint.test.ts Adds SCSS test coverage for unitlessZeroInMath and updated zero-in-math behavior, including preprocessor-specific cases.
src/​test/​css/​lint.test.ts Adds CSS tests validating the updated zeroUnits suppression and new unitlessZeroInMath warnings in typed math expressions.
src/​services/​lintRules.ts Registers the new unitlessZeroInMath lint rule and defines its default diagnostic messaging/severity.
src/​services/​lint.ts Implements math-function detection updates for zeroUnits and adds typed analysis to flag unitless zeros that must be dimensions in math contexts.

Review effort: Balanced

Comment thread src/services/lint.ts Resolved
Comment thread src/services/lint.ts Resolved

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reproduced issues to address before merging

1. High priority: exponential validation work for nested calculations. productType() calls mathType() when filtering non-number factors, then calls it again on the same dimension factor. Nested parenthesized expressions multiply this repeated traversal.

Reproduction: generate valid CSS with const text = "a { width: calc(" + "(".repeat(20) + "1px" + ")".repeat(20) + "); }";, parse it with the CSS language service, then time doValidation() alone. On this machine the current main completed in about 1.6 ms, while this head exceeded a 3-second subprocess timeout. At depth 16 this head took about 344 ms. There are no zero values in this input. Please cache per-node type results or otherwise ensure bounded traversal, and add a focused nested-expression performance regression.

Location: src/services/lint.ts, productType() around lines 70-75 and recursive mathType().

2. The new warning flags valid Sass even without Sass variables. SCSS validation now reports unitlessZeroInMath for both:

a { width: max(0, 1px); }
a { width: calc(0); }

Sass permits unit/unitless mixing in legacy-compatible min()/max() calculations, and simplifies calc(0) to the ordinary number 0. Skipping expressions only when they contain preprocessor variables therefore does not prevent these false positives. Please make the inference dialect-aware and add SCSS cases without variables. Sass documents the calculation simplification and legacy compatibility rules here: https://sass-lang.com/documentation/values/calculations/

These are separate from the earlier optional-chaining comment: the author's rebuttal to that comment is correct; I did not reproduce a null-declaration crash.

Reviewed commit: c0b8d67. Existing full tests pass, but do not cover these cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants